package vg.modules.notepad;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventCloseAIF;
import vg.services.plugin_manager.event_and_request.event.UIEventCreateNewConnection;
import vg.services.plugin_manager.event_and_request.event.UIEventDeleteConnection;
import vg.services.plugin_manager.event_and_request.request.AUIRequest;
import vg.services.plugin_manager.event_and_request.request.UIRequestGoToInAIF;
import vg.services.plugin_manager.event_and_request.request.UIRequestLoadProject;
import vg.services.plugin_manager.event_and_request.request.UIRequestSaveProject;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.UserInterfaceObserverAdapter;

/**
 * Plugin realizes notepad for reading files.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class NotepadPlugin implements IPlugin {
	public void install(final PluginParameter param) throws Exception {
		final Notepad notepad = new Notepad(param);
		final JMenuItem jMenuItem = new JMenuItem("Notepad");
		jMenuItem.setIcon(new ImageIcon("./data/resources/textures/notepad/notepad_2.png"));
		jMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				notepad.open();
			}
		});
		jMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK));
        param.userInterfaceManager.addMenuItem(jMenuItem, "edit");

        param.userInterfaceManager.addObserver(new UserInterfaceObserverAdapter() {
            @Override
            public void dispatchEvent(AUIEvent event) {
                switch (event.getType()) {
                    case DEF_CLOSE_PROGRAM: {
                        notepad.dispose();
                        break;
                    }
                    case DEF_CHANGE_UI_STYLE: {
                        notepad.updateUITheme();
                        break;
                    }
                    case DEF_CLOSE_AIF: {
                        notepad.closeAIF((UIEventCloseAIF)event);
                        break;
                    }
                    case DEF_CREATE_NEW_CONNECTION: {
                        notepad.addConnection((UIEventCreateNewConnection)event);
                        break;
                    }
                    case DEF_DELETE_CONNECTION: {
                        notepad.deleteConnection((UIEventDeleteConnection)event);
                        break;
                    }
                    case DEF_RESET: {
                        notepad.reset();
                        break;
                    }
                }
            }

            @Override
            public void dispatchRequest(AUIRequest request) {
                switch(request.getType()) {
                    case DEF_GOTO_IN_AIF: {
                        notepad.goToAIF((UIRequestGoToInAIF)request);
                        break;
                    }
                    case DEF_SAVE_PROJECT: {
                        notepad.saveProject(((UIRequestSaveProject)request).getPath());
                        break;
                    }
                    case DEF_LOAD_PROJECT: {
                        notepad.loadProject(((UIRequestLoadProject)request).getPath());
                        break;
                    }
                }
            }
        });
	}
}
